import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { BusinessException } from 'src/common/exceptions/business.exception'
import { Asset } from 'src/entity/Asset'
import { Fix } from 'src/entity/Fix'
import { Task } from 'src/entity/Task'
import { Connection, IsNull, Repository } from 'typeorm'
import { v4 as uuidV4 } from 'uuid'

@Injectable()
export class FixService {
  constructor(
    @InjectRepository(Fix)
    private fixRepository: Repository<Fix>,
    private connection: Connection,
  ) { }

  async add(data) {
    const fixCode = 'F' + uuidV4()
    data.fixCode = fixCode
    data.fixType = '0'

    await this.connection.transaction(async (manager) => {
      const assetSqlData = await manager.findOne(Asset, {
        where: {
          assetCode: data.assetCode,
        },
      })
      if (!assetSqlData) throw new BusinessException('该资产编号不存在')
      if (assetSqlData.isDelete)
        throw new BusinessException('该资产已经出库，不能维修')
      data.assetName = assetSqlData.assetName
      data.fixTypeId = assetSqlData.typeId
      const sqlData = await manager.save(Fix, data)
      // 资产改为维修中
      await manager.update(
        Asset,
        {
          assetCode: data.assetCode,
        },
        {
          assetStatus: '6',
        },
      )
      return sqlData
    })

    return data
  }

  async del(id: number) {
    let data
    await this.connection.transaction(async (manager) => {
      data = await manager.update(
        Fix,
        {
          id: id,
        },
        {
          isDelete: true,
        },
      )
    })
    return data
  }

  async update(data) {
    await this.connection.transaction(async (manager) => {
      await manager.update(
        Fix,
        {
          id: data.id,
        },
        data,
      )
      // 改变资产状态 维修中 维修失败
      if (data.fixType === '1' || data.fixType === '2') {
        await manager.update(
          Asset,
          {
            assetCode: data.assetCode,
          },
          {
            assetStatus: data.fixType === '1' ? '7' : '1',
            isDelete: data.fixType === '1' ? true : null,
          },
        )
      }
    })
    const sqlData = await this.fixRepository.update(
      {
        id: data.id,
      },
      data,
    )
    return sqlData
  }

  // 分页查询
  async getPageList({
    pageRequest: { pageIndex, pageSize },
    fixCode,
    assetName,
    fixTypeId,
    receivePerson,
    receiveBranch,
    fixType,
    orgId,
  }) {
    const sqlAllData = await this.fixRepository.find({
      where: {
        isDelete: IsNull(),
        receiveBranchId: orgId,
        fixCode: fixCode,
        assetName: assetName,
        fixTypeId: fixTypeId ? parseInt(fixTypeId) : undefined,
        receivePerson: receivePerson,
        receiveBranch: receiveBranch,
        fixType: fixType,
      },
    })

    const sqlData = await this.fixRepository.find({
      where: {
        isDelete: IsNull(),
        receiveBranchId: orgId,
        fixCode: fixCode,
        assetName: assetName,
        fixTypeId: fixTypeId ? parseInt(fixTypeId) : undefined,
        receivePerson: receivePerson,
        receiveBranch: receiveBranch,
        fixType: fixType,
      },
      skip: (pageIndex - 1) * pageSize,
      take: pageSize,
      withDeleted: false,
    })

    return {
      content: sqlData.map((item) => {
        return {
          ...item,
          fixTypeId: item.fixTypeId.toString(),
        }
      }),
      total: sqlAllData.length,
    }
  }

  // 获取维修详情 fixCode -> *
  async getDetailByCode(code) {
    const sqlData = await this.fixRepository.findOne({
      where: {
        fixCode: code,
        isDelete: IsNull(),
      },
    })
    return {
      ...sqlData,
      fixTypeId: sqlData.fixTypeId.toString(),
    }
  }

  // 获取维修详情
  async getFixDetail(id) {
    return await this.fixRepository.findOne({
      where: {
        isDelete: IsNull(),
        id,
      },
    })
  }
}
